package common;

import util.Thread1;
import util.Thread2;

public class TestCommonEntity {
	private String ss = "locker";
	public int i = 0;
	public int j = 0;
	int label = 0;
	String name = null;
	String sex = null;
	boolean flag = false;
	boolean flag2 = false;
	boolean flag3 = false;
	boolean flag4 = false;

	public void set(TestCommonEntity testCommonEntity, String name) {
		synchronized (ss) {
			System.out.println("===go notify===");
			ss.notify();
			System.out.println("===notified===");
		}
		synchronized (ss) {
			// System.out.println("===go waiting===");
			// wait();
			// System.out.println("===waked===");
			testCommonEntity.i = testCommonEntity.i + 1;
			try {
				System.out.println("==go waiting===");
				ss.wait();
				System.out.println("==wake===");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("value in" + name + " is:" + testCommonEntity.i);
		}
		synchronized (ss) {
			System.out.println("===go notify===");
			ss.notify();
			System.out.println("===notified===");
		}
	}

	public synchronized void set2(String name, String sex) {
		if (flag) {
			try {
				System.out.println("===waiting in set====:" + ++j);
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.name = name;
		this.sex = sex;
		this.notify();
		flag = true;
	}

	public synchronized void get() {
		if (!flag) {
			try {
				System.out.println("==waiting in get===:" + ++i);
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("name:" + name + ",sex:" + sex);
		this.notify();
		flag = false;
	}

	public synchronized void set3() {
		System.out.println("set3");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public synchronized void get3() {
		System.err.println("get2");
	}

	public synchronized void show1(String name) {
		// if (flag) {
		// try {
		// this.wait();
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		// System.out.println(name);
		// this.notify();
		// flag = true;
		if (flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (flag3) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(name);
		flag = true;

		flag3 = true;
		flag2 = true;
		this.notifyAll();
	}

	public synchronized void show2(String name) {
		// if (!flag) {
		// try {
		// this.wait();
		// } catch (Exception e) {
		// // TODO: handle exception
		// }
		// }
		// System.out.println(name);
		// this.notify();
		// flag = false;
		if (!flag2) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (!flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("2");

		flag3 = true;
		flag2 = false;

		flag = false;
		this.notifyAll();
	}

	public synchronized void show3(String name) {
		if (!flag3) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (flag2) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		flag = false;
		System.out.println(name);
		flag2 = true;
		flag3 = false;
		this.notifyAll();
	}

	public synchronized void show(String name) {
		if ("1".equals(name)) {
			if (flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (flag3) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(name);
			flag = true;

			flag3 = true;
			flag2 = false;
			this.notifyAll();
		} else if ("2".equals(name)) {
			if (flag2) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("2");

			flag3 = false;
			flag2 = true;

			flag = false;
			this.notifyAll();
		} else if ("3".equals(name)) {
			if (!flag3) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag2) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			flag = false;
			System.out.println(name);
			flag2 = true;
			flag3 = false;
			this.notifyAll();
		}

	}

	public synchronized void show4(String name) {
		if ("1".equals(name)) {
			if (flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (flag3) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (flag2) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(name);
			flag = true;

			flag3 = true;
			flag2 = true;
			flag4 = true;
			this.notifyAll();
		} else if ("2".equals(name)) {
			if (flag2) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (flag4) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("2");

			flag3 = true;
			flag2 = false;
			flag = false;
			this.notifyAll();
		} else if ("3".equals(name)) {
			if (flag3) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag2) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag4) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			flag = false;
			System.out.println(name);
			flag2 = true;
			flag3 = true;
			this.notifyAll();
		} else if ("4".equals(name)) {
			if (!flag4) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag3) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			flag = false;
			System.out.println(name);
			flag2 = true;
			flag3 = false;
			this.notifyAll();
		}

	}

}
